package com.dd.ent.car.common.player;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.WindowManager;

import com.dd.ent.car.R;
import com.dd.ent.car.common.util.DipPixelUtil;
import com.dd.ent.car.common.util.NetworkHelper;

/**
 * Created by wx on 2016/3/8.
 */
public class FeedDataUtils {

    private static String millisecondToClickString(int millisecond) {

        if(millisecond < 0) return "00:00";

        int second = millisecond / 1000;
        if(second == 0) return "00:00";
        int minute = second / 60;
        if(minute == 0) {
            return "00:"+doubleDigit(second);
        }
        int hour = minute / 60;
        int secondDigit = second % 60;
        if(hour == 0) {

            return doubleDigit(minute)+":"+doubleDigit(secondDigit);
        }else {
            int minuteDigit = minute % 60;
            return doubleDigit(minuteDigit)+":"+doubleDigit(secondDigit);
        }

    }

    private static String doubleDigit(int time){
        if(time < 10) {
            return "0"+time;
        }else {
            return ""+time;
        }
    }

    /**
     * 使背景音乐静音(暂停)
     * @param context
     * @param bMute 是否静音，true 静音
     * @return
     */
    public static boolean muteAudioFocus(Context context, boolean bMute) {
        if(context == null){
            return false;
        }
        if(android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.FROYO){
            // 2.1以下的版本不支持下面的API：requestAudioFocus和abandonAudioFocus
            return false;
        }
        boolean bool = false;
        AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
        if(bMute){
            int result = am.requestAudioFocus(null,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
            bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED; // 不得获取音频焦点
        }else{
            int result = am.abandonAudioFocus(null);
            bool = result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED;
        }
        return bool;
    }

    /**
     * 点赞数大于10W之后的格式化方法
     * @param clickNiceNum
     * @return 例如：123656 -->12.4W, 100023 -->10W, 小于10W的正常显示
     */
    public static String formatClickNiceNum(int clickNiceNum) {
        if (clickNiceNum >= 100000) {
            float f1 = (float)(Math.round(clickNiceNum))/10000;
            float f2 = (float)(Math.round(f1*10))/10;

            if (f2*10%10 == 0) {
                int m = (int)f2*10/10;
                return m+"万";
            }
            return f2+"万";
        }
        return clickNiceNum+"";
    }

    /**
     * 要播放网络视频，必须先检测网络状态，非wifi网络下，提示用户后，才可播放，wifi下直接播放
     * @param context
     * @return
     */
    public static boolean checkNetThenVideoPlay(Context context, final OnCheckNetDialogListener onCheckNetDialogListener) {
        if (NetworkHelper.isNetworkAvailable(context)) { // 判断是否有网络可用
            if (NetworkHelper.isActiveNetworkWifi(context)) { // 如果是WiFi网络，视频直接播放
                return true;
            } else {
                // 不是WiFi网络就弹dialog，在弹dialog之前还要判断用户是否点击过继续播放，如果点击过就不显示dialog，直接播放视频
                if (FeedPlayerParameters.instance().isNotCheckMobileNetAgain()) {
                    return true;
                } else {
                    showMobileNetNotifyDialog(context, onCheckNetDialogListener);
                }
            }

        } else {
           // XLToast.showCenterToast(context, XLToast.XLToastType.XLTOAST_TYPE_ALARM, "无网络连接");
            return false;
        }
        return false;
    }

    /**
     * MobileNet下显示dialog
     * @param context
     * @param onCheckNetDialogListener
     */
    public static void showMobileNetNotifyDialog(Context context, final OnCheckNetDialogListener onCheckNetDialogListener) {
        String contentStr = context.getString(R.string.feed_wifi_notify_tag_has_mobile);

        // 左按钮--停止播放
        String leftBtnStr = context.getString(R.string.feed_wifi_notify_btn_stop);
        DialogInterface.OnClickListener lListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                xLTwoButtonDialog = null;
                onCheckNetDialogListener.OnCheckNetDialogLeftBtnListener();
            }
        };

        // 右按钮 --继续播放
        String rightBtnStr = context.getString(R.string.feed_wifi_notify_btn_continue);
        DialogInterface.OnClickListener rListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                FeedPlayerParameters.instance().setNotCheckMobileNetAgain(true);
                dialog.dismiss();
                xLTwoButtonDialog = null;
                onCheckNetDialogListener.OnCheckNetDialogRightBtnListener();
            }
        };

        // 物理返回键
        DialogInterface.OnCancelListener cListener = new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog.dismiss();
                xLTwoButtonDialog = null;
                onCheckNetDialogListener.OnCheckNetDialogBackBtnListener();
            }
        };

        FeedDataUtils.showWifiNotifyDialog(contentStr, leftBtnStr, rightBtnStr, lListener, rListener, cListener, context);
    }

    /**
     * 显示NotifyDialog
     */
    private static XLAlarmDialog xLTwoButtonDialog;
    public static void showWifiNotifyDialog(String contentStr, String leftBtnStr, String rightBtnStr,
                                            DialogInterface.OnClickListener lListener, DialogInterface.OnClickListener rListener,
                                            DialogInterface.OnCancelListener cListener, final Context context) {

        if (xLTwoButtonDialog != null && xLTwoButtonDialog.isShowing() && context == xLTwoButtonDialog.getOwnerActivity()) {
            return;
        }
        if (xLTwoButtonDialog == null || xLTwoButtonDialog.getOwnerActivity() != context) {
            xLTwoButtonDialog = new XLAlarmDialog(context);
            if(context instanceof Activity){
                xLTwoButtonDialog.setOwnerActivity((Activity)context);
            }
        }
        xLTwoButtonDialog.setCancelable(false);
        xLTwoButtonDialog.setCanceledOnTouchOutside(false);
        xLTwoButtonDialog.setContent(contentStr);
        xLTwoButtonDialog.setCancelButtonText(leftBtnStr);
        xLTwoButtonDialog.setOnClickCancelButtonListener(lListener);
        xLTwoButtonDialog.setConfirmButtonText(rightBtnStr);
        xLTwoButtonDialog.setOnClickConfirmButtonListener(rListener);
        if (cListener != null) {
            xLTwoButtonDialog.setOnCancelListener(cListener);
            xLTwoButtonDialog.setCancelable(true);
        }
        // 设置XLAlarmDialog宽度
        xLTwoButtonDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                Dialog dialog = (Dialog) dialogInterface;
                WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
                lp.width = DipPixelUtil.dip2px(context, 314);
                dialog.getWindow().setAttributes(lp);
            }
        });

        if (xLTwoButtonDialog.isShowing()) {
            return;
        }
        xLTwoButtonDialog.show();
    }

    public static boolean isNetworkTypeMobile(ConnectivityManager connectivityManager) {
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info == null) {
            return false;
        }

        switch (info.getType()) {
            case ConnectivityManager.TYPE_MOBILE:
            case ConnectivityManager.TYPE_MOBILE_MMS:
            case ConnectivityManager.TYPE_MOBILE_SUPL:
            case ConnectivityManager.TYPE_MOBILE_DUN:
            case ConnectivityManager.TYPE_MOBILE_HIPRI:
                return true;
        }

        return false;
    }

    public static boolean isNetworkTypeWifi(ConnectivityManager connectivityManager) {
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info == null) {
            return false;
        }

        switch (info.getType()) {
            case ConnectivityManager.TYPE_WIFI:
                return true;
        }

        return false;
    }
    public interface OnCheckNetDialogListener {
        void OnCheckNetDialogLeftBtnListener();
        void OnCheckNetDialogRightBtnListener();
        void OnCheckNetDialogBackBtnListener();
    }

}
